package controllers;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.sun.xml.internal.stream.Entity;

import play.*;
import play.mvc.*;

import models.EEntity;
import models.Notification;
import models.Organization;
import models.Request;
import models.Role;
import models.RoleInEntity;
import models.RoleInOrganization;
import models.Topic;
import models.User;

/**
 * 
 * @author Shady Emil
 * @version 1.0
 * 
 */
public class RequestController extends Controller {

	/**
	 * The requestJoin action method given a user ID and an organization ID will
	 * send a request to join the organization from the user given
	 * 
	 * 
	 * @param userID
	 *            The id of the user who sent the request
	 * @param organizationID
	 *            The id of the organization to which the request will be sent
	 */
	public static void requestJoin(long userID, long organizationID) {
		User user;

		String userName = session.get("username");
		if (userName != null) {
			user = User.find("byUserName", userName).first();
			if (userID == user.id) {
				Organization.requestJoin(userID, organizationID);
			}
		}
	}

	/**
	 * The approveJoin action method given a user ID and an organization ID and
	 * the user who made the request will approve a request to join the
	 * organization.
	 * 
	 * @param organizationLeadID
	 *            The id of the org. lead who will approve the request
	 * @param organizationID
	 *            The id of the organization to which the request was sent
	 * @param requestingUserID
	 *            The id of the user who issued the request
	 */
	public static void approveJoin(long organizationLeadID,
			long organizationID, long requestingUserID) {
		User user;

		String userName = session.get("username");
		if (userName != null) {
			user = User.find("byUserName", userName).first();
			if (user.id == organizationLeadID) {
				Organization.approveJoin(organizationLeadID, organizationID,
						requestingUserID);
			}
		}
	}

	// for testing only will be removed
	public static void requestJoinTest(long userID, long organizationID) {

		Organization.requestJoin(userID, organizationID);

	}

	// for testing only will be removed
	public static void approveJoinTest(long organizationLeadID,
			long organizationID, long requestingUserID) {

		Organization.approveJoin(organizationLeadID, organizationID,
				requestingUserID);

	}

	/**
	 * @author Hesham Al-Razaz 
	 * 
	 * 
	 * 
	 * This method is used to view the invitations to
	 *         the user
	 * 
	 */

	public static void showRequests() {
		String userName = session.get("username");
		int ifUserNull = 0;

		User user = null;
		List<Request> allRequests = null;
		boolean size = true;

		user = User.find("byUsername", userName).first();
		if (user == null)
			ifUserNull = 1;

		else {
			allRequests = user.invitations;

			if (allRequests.size() == 0)
				size = false;
		}
		render(allRequests, size, ifUserNull);

	}

	/**
	 * @author Hesham Al-Razaz
	 * @param orgID
	 *            Organization ID
	 * @param entID
	 *            Entity ID
	 * @param reqID
	 *            request ID
	 * 
	 *            This method is done when the user accepts a request It adds
	 *            the user as a member to the Organization/Entity he is invited
	 *            too It removes the request from the list of requests of this
	 *            user after adding him and notifies the organizer that the user
	 *            accepted the invitation
	 */

	public static void acceptRequest(long orgID, long entID, long reqID) {

		String userName = session.get("username");
		if (userName == null)
			return;
		User user = null;
		user = User.find("byUsername", userName).first();
		if (user == null)
			return;

		Request request = Request.findById(reqID);

		if (entID == -1) {

			Organization organization = Organization.findById(orgID);

			Role role = new Role("IdeaDeveloper");
			role.save();
			RoleInOrganization roleInOrganization = new RoleInOrganization(
					organization, user, role);
			roleInOrganization.save();

			organization.addRoleAndUser(roleInOrganization);
			organization.save();
			user.save();

			LinkedList<User> toBeNotified = new LinkedList<User>();
			toBeNotified.add(request.sendingUser);
			Notification newNotification = new Notification(
					" accepted your invitation to " + organization.name, reqID,
					14, request.recievingUser, toBeNotified);
			newNotification.timeStamp = new Date();
			newNotification.save();

			request.delete();

		} else {

			EEntity entity = EEntity.findById(entID);

			Role role = new Role("IdeaDeveloper");
			role.save();

			RoleInOrganization roleInOrganization = new RoleInOrganization(
					entity.organization, user, role);
			roleInOrganization.save();

			entity.organization.addRoleAndUser(roleInOrganization);
			entity.organization.save();

			RoleInEntity roleInEntity = new RoleInEntity(entity, user, role);

			roleInEntity.save();
			entity.addRoleAndUser(roleInEntity);
			entity.save();
			user.save();
			LinkedList<User> toBeNotified = new LinkedList<User>();
			toBeNotified.add(request.sendingUser);

			Notification newNotification = new Notification(
					"accepted your invitation to " + entity.name, reqID, 14,
					request.recievingUser, toBeNotified);
			newNotification.timeStamp = new Date();
			newNotification.save();

			request.delete();

		}

		showRequests();

	}

	/**
	 * @author Hesham Al-Razaz
	 * 
	 * @param reqID
	 *            request ID
	 * 
	 *            This method is done when the user ignores an invitation,It
	 *            just removes the request from the list of requests of this
	 *            user and notifies the organizer that the user ignored the
	 *            invitation
	 */
	public static void ignoreRequest(long orgID, long entID, long reqID) {
		String userName = session.get("username");
		if (userName == null)
			return;
		User user = null;
		user = User.find("byUsername", userName).first();

		if (user == null) {

			return;
		}
		Request request = Request.findById(reqID);
		user.save();

		if (entID == -1) {

			Organization organization = Organization.findById(orgID);
			if (organization == null)
				return;
			LinkedList<User> toBeNotified = new LinkedList<User>();
			toBeNotified.add(request.sendingUser);

			Notification newNotification = new Notification(
					"ignored your invitation to " + organization.name, reqID,
					14, request.recievingUser, toBeNotified);
			newNotification.timeStamp = new Date();
			newNotification.save();

		} else {

			EEntity entity = EEntity.findById(entID);
			if (entity == null)
				return;
			LinkedList<User> toBeNotified = new LinkedList<User>();
			toBeNotified.add(request.sendingUser);

			Notification newNotification = new Notification(
					"ignored your invitation to " + entity.name, reqID, 14,
					request.recievingUser, toBeNotified);
			newNotification.timeStamp = new Date();
			newNotification.save();

		}

		request.delete();

		showRequests();

	}

	/**
	 * check whether the requesting resign user is logged in and also that he is
	 * actually the organization lead of the given organization.It also checks
	 * if he has already a similar pending request.After these validations it
	 * then renders html page in which the users fills in the name of the user
	 * he wants to take over his position after resign.
	 * 
	 * @author Omar Darwish
	 * @param orgID
	 *            The id of the organization that the organization lead want to
	 *            resign from being its lead.
	 * @see depends on the chesks but mainly a page where the user enters the
	 *      suggested new lead or if there is any some thing wrong in the
	 *      validation a page is rendered clarifying the error occurred.
	 * @story C1-S17
	 */
	public static void organizationLeadResignRequest(long orgID) {
		int cancelRequest = -1;
		String userName = session.get("username");
		if (userName == null) {
			render(cancelRequest);
		}
		User user = User.find("byUsername", userName).first();
		if (user == null) {
			render(cancelRequest);
		}
		Organization organization = Organization.findById(orgID);
		if (organization == null
				|| user.id != organization.getOrganizationLead().id) {
			render(cancelRequest);
		}

		List<Request> requests = Request.findAll();
		boolean found = false;
		for (Request item : requests) {
			if (item.organization != null
					&& item.organization.id == orgID
					&& item.sendingUser.id == user.id
					&& item.description != null
					&& item.description
							.equalsIgnoreCase("OrganizationLeadResign")) {
				found = true;
				break;
			}
		}
		if (found) {
			cancelRequest = 1;
			render(cancelRequest, orgID);
		} else {
			cancelRequest = 0;
			LinkedList<String> userNamesAndEmails = new LinkedList<String>();
			List<User> allUsers = User.findAll();
			for (User item : allUsers) {
				if (item.id == user.id || item.isSysteAdmin())
					continue;
				userNamesAndEmails.add(item.userName);
				userNamesAndEmails.add(item.email);
			}
			render(cancelRequest, orgID, userNamesAndEmails);
		}
	}

	/**
	 * cancels a pending request previously done by a user to resign as an
	 * organization lead from certain organization and invite another one to be
	 * in his position.
	 * 
	 * @author Omar Darwish
	 * @param orgID
	 *            The id of the organization that the organization lead want to
	 *            resign from being its lead.
	 * @see an html page where the user submit a request for resign and enters
	 *      the name of the new suggested lead.
	 * @story C1-S17
	 */
	public static void cancelOrganizationLeadResignRequest(long orgID) {
		String userName = session.get("username");
		if (userName == null) {
			error("You are not authorized to view this page");
		}
		User user = User.find("byUsername", userName).first();
		if (user == null) {
			error("You are not authorized to view this page");
		}
		List<Request> requests = Request.findAll();
		for (Request item : requests) {
			if (item.organization != null
					&& item.organization.id == orgID
					&& item.sendingUser.id == user.id
					&& item.description != null
					&& item.description
							.equalsIgnoreCase("OrganizationLeadResign")) {
				item.delete();
				break;
			}
		}
		organizationLeadResignRequest(orgID);
	}

	/**
	 * saves a request from a user as organization lead to resign from his
	 * position and sends an email to the suggested new lead.
	 * 
	 * @author Omar Darwish
	 * @param orgID
	 *            The id of the organization that the organization lead want to
	 *            resign from being its lead.
	 * @param nameOrEmail
	 *            The name or the email of the new suggested lead as specified
	 *            by the old lead.
	 * @see a page specifying that the request has been saved and an invitation
	 *      is sent to the suggested new lead.
	 * @story C1-S17
	 */
	public static void sendOrganizationLeadResignRequest(long orgID,
			String nameOrEmail) {
		String userName = session.get("username");
		if (userName == null) {
			error("You are not authorized to view this page");
		}
		User sendingUser = User.find("byUsername", userName).first();
		if (sendingUser == null) {
			error("You are not authorized to view this page");
		}
		User recievingUser = null;
		int flag = 0;
		if (nameOrEmail != null) {
			if (!nameOrEmail.contains("@")) {
				recievingUser = User.find("byUsername", nameOrEmail).first();
			} else {
				recievingUser = User.find("byEmail", nameOrEmail).first();
			}
			if (recievingUser == null) {
				flag = 1;
			} else if (recievingUser.id == sendingUser.id) {
				flag = 2;
			}
		}
		validation.required(nameOrEmail)
				.message("You must choose the new lead");
		if (flag == 1) {
			validation.addError(nameOrEmail,
					"You must a choose an existing user");
		} else if (flag == 2) {
			validation.addError(nameOrEmail,
					"You can't choose yourself (This is redundant!!!)");
		}
		if (validation.hasErrors()) {
			params.flash();
			validation.keep();
			organizationLeadResignRequest(orgID);
		} else {
			Organization organization = Organization.findById(orgID);
			if (organization == null) {
				error("You are not authorized to view this page");
			}

			List<Request> requests = Request.findAll();
			boolean found = false;
			for (Request item : requests) {
				if (item.organization != null
						&& item.organization.id == orgID
						&& item.sendingUser.id == sendingUser.id
						&& item.description != null
						&& item.description
								.equalsIgnoreCase("OrganizationLeadResign")) {
					found = true;
					break;
				}
			}
			if (found) {
				int cancelRequest = 1;
				renderTemplate(
						"/RequestController/organizationLeadResignRequest.html",
						cancelRequest, orgID);
			}

			Request request = new Request(sendingUser, null, organization,
					recievingUser, new Date(), "OrganizationLeadResign").save();
			String message = "The organization lead of the organization "
					+ organization.name
					+ " wants to resign and suggested you to be this organization lead "
					+ "\n Please log in and go to this link to accept ot reject the request:-"
					+ "\n http://localhost:9000/requestcontroller/showrequests";
			Application.sendMail(message, "Organization Lead Replacement",
					recievingUser);
			int flag2 = 1;
			renderTemplate("/RequestController/sendResignRequest.html", orgID,flag2);
		}
	}

	/**
	 * accepts a resign request from a specific organization lead.So it makes
	 * the old lead just an idea developer in the organization and assign the
	 * new lead the organization lead role with notifying the old lead.
	 * 
	 * @author Omar Darwish
	 * @param requestID
	 *            The id of the request that was accepted.
	 * @see a page specifying that the request has been accepted and the user
	 *      has been the organization new lead.
	 * @story C1-S17
	 */
	public static void acceptOrganizationLeadResignRequest(long requestID) {
		String userName = session.get("username");
		if (userName == null) {
			error("You are not authorized to view this page");
		}
		User user = User.find("byUsername", userName).first();
		Request request = Request.findById(requestID);
		if (user == null || request == null || request.recievingUser != user) {
			error("You are not authorized to view this page");
		}
		Role lead = Role.find("byDescription", "OrganizationLead").first();
		Role ideaDeveloper = Role.find("byDescription", "IdeaDeveloper")
				.first();
		RoleInOrganization oldRoleOfOldLead = RoleInOrganization.find(
				"byRoleAndOrganizationAndUser", lead, request.organization,
				request.sendingUser).first();
		oldRoleOfOldLead.role = ideaDeveloper;
		oldRoleOfOldLead.save();
		List<RoleInOrganization> allRoles = RoleInOrganization.findAll();
		boolean flag = false;
		for (RoleInOrganization item : allRoles) {
			if (item.organization.id == request.organization.id
					&& item.user.id == request.recievingUser.id) {
				item.role = lead;
				item.save();
				flag = true;
				break;
			}
		}
		if (!flag) {
			RoleInOrganization newRole = new RoleInOrganization(
					request.organization, request.recievingUser, lead).save();
		}

		String message = "You are no longer the organization lead of  "
				+ request.organization.name;
		Application.sendMail(message, "Organization Lead Replacement",
				request.sendingUser);
		Organization organization = request.organization;
		request.delete();
		int type = 1;
		renderTemplate("/RequestController/acceptResignRequest.html",
				organization, type);
	}

	/**
	 * ignores a resign request from a specific organization lead and notifies
	 * the old lead that the suggested one ignored the request.So the old lead
	 * stays in his position
	 * 
	 * @author Omar Darwish
	 * @param requestID
	 *            The id of the request that was ignored.
	 * @see just the page that shows the requests of the navigating user.
	 * @story C1-S17
	 */
	public static void ignoreOrganizationLeadResignRequest(long requestID) {
		String userName = session.get("username");
		if (userName == null) {
			error("You are not authorized to view this page");
		}
		User user = User.find("byUsername", userName).first();
		Request request = Request.findById(requestID);
		if (user == null || request == null) {
			error("You are not authorized to view this page");
		}
		String message = "The suggested user ignored you request so you are still  "
				+ "the organization lead of  "
				+ request.organization.name
				+ " organization";
		Application.sendMail(message, "Organization Lead Replacement",
				request.sendingUser);
		request.delete();
		showRequests();
	}

	/**
	 * checks whether the requesting resign user is logged in and also that he
	 * is actually an organizer in the given entity.It also checks if he has
	 * already a similar pending request.If he is not the last organizer in this
	 * entity the request is accepted directly and mails are sent notifying the
	 * lead of the organization containing the entity and all other organizers.
	 * Otherwise it renders html page in which the users fills in the name of
	 * the user he wants to take over his position after resign.
	 * 
	 * @author Omar Darwish
	 * @param entityID
	 *            The id of the entity that the user want to resign from being
	 *            its organizer.
	 * @see depends on the chesks but mainly a page where the user enters the
	 *      suggested new organizer or if there is any some thing wrong in the
	 *      validation a page is rendered clarifying the error occurred.Also if
	 *      he is not the last organizer in the entity a request accepted page
	 *      is rendered
	 * @story C1-S20
	 */
	public static void organizerResignRequest(long entityID) {
		int cancelRequest = -1;
		String userName = session.get("username");
		if (userName == null) {
			render(cancelRequest);
		}
		User user = User.find("byUsername", userName).first();
		if (user == null) {
			render(cancelRequest);
		}
		EEntity entity = EEntity.findById(entityID);
		if (entity == null || !entity.getOrganizers().contains(user)) {
			render(cancelRequest);
		}
		List<Request> requests = Request.findAll();
		boolean found = false;
		for (Request item : requests) {
			if (item.entity != null && item.entity.id == entityID
					&& item.sendingUser.id == user.id
					&& item.description != null
					&& item.description.equalsIgnoreCase("OrganizerResign")) {
				found = true;
				break;
			}
		}
		if (found) {
			cancelRequest = 1;
			render(cancelRequest, entity);
		} else {
			if (entity.getOrganizers().size() == 1) {
				cancelRequest = 0;
				LinkedList<String> userNamesAndEmails = new LinkedList<String>();
				List<User> allUsers = User.findAll();
				for (User item : allUsers) {
					if (item.id == user.id || item.isSysteAdmin())
						continue;
					userNamesAndEmails.add(item.userName);
					userNamesAndEmails.add(item.email);
				}
				render(cancelRequest, entity, userNamesAndEmails);
			} else {
				cancelRequest = 2;
				Role organizer = Role.find("byDescription", "Organizer")
						.first();
				Role ideaDeveloper = Role
						.find("byDescription", "IdeaDeveloper").first();
				RoleInEntity oldRole = RoleInEntity.find(
						"byRoleAndEntityAndUser", organizer, entity, user)
						.first();
				List<RoleInEntity> y = RoleInEntity.findAll();
				oldRole.role = ideaDeveloper;
				oldRole.save();
				List<User> Notified = entity.getOrganizers();
				Notified.add(entity.organization.getOrganizationLead());
				String message = user.name
						+ " chose to be no longer an organizer in "
						+ entity.name + " entity";
				for (User item : Notified) {
					Application.sendMail(message, "Organizer resign", item);
				}
				render(cancelRequest, entity);
			}
		}
	}

	/**
	 * cancels a pending request previously done by a user to resign as an
	 * organizer from certain entity and invite another one to be in his
	 * position.
	 * 
	 * @author Omar Darwish
	 * @param entityID
	 *            The id of the entity that the user want to resign from being
	 *            its organizer.
	 * @see an html page where the user submit a request for resign and enters
	 *      the name of the new suggested organizer.
	 * @story C1-S20
	 */
	public static void cancelOrganizerResignRequest(long entityID) {
		String userName = session.get("username");
		if (userName == null) {
			error("You are not authorized to view this page");
		}
		User user = User.find("byUsername", userName).first();
		if (user == null) {
			error("You are not authorized to view this page");
		}
		List<Request> requests = Request.findAll();
		for (Request item : requests) {
			if (item.entity != null && item.entity.id == entityID
					&& item.sendingUser.id == user.id
					&& item.description != null
					&& item.description.equalsIgnoreCase("OrganizerResign")) {
				item.delete();
				break;
			}
		}
		organizerResignRequest(entityID);
	}

	/**
	 * saves a request from a user as organizer to resign from his position and
	 * sends an email to the suggested new organizer.
	 * 
	 * @author Omar Darwish
	 * @param entityID
	 *            The id of the entity that the user want to resign from being
	 *            its organizer.
	 * @param nameOrEmail
	 *            The name or the email of the new suggested organizer as
	 *            specified by the old organizer.
	 * @see a page specifying that the request has been saved and an invitation
	 *      is sent to the suggested new organizer.
	 * @story C1-S20
	 */
	public static void sendOrganizerResignRequest(long entityID,
			String nameOrEmail) {
		String userName = session.get("username");
		if (userName == null) {
			error("You are not authorized to view this page");
		}
		User sendingUser = User.find("byUsername", userName).first();
		if (sendingUser == null) {
			error("You are not authorized to view this page");
		}
		User recievingUser = null;
		int flag = 0;
		if (nameOrEmail != null) {
			if (!nameOrEmail.contains("@")) {
				recievingUser = User.find("byUsername", nameOrEmail).first();
			} else {
				recievingUser = User.find("byEmail", nameOrEmail).first();
			}
			if (recievingUser == null) {
				flag = 1;
			} else if (recievingUser.id == sendingUser.id) {
				flag = 2;
			}
		}
		validation.required(nameOrEmail).message(
				"You must choose the new Organizer");
		if (flag == 1) {
			validation.addError(nameOrEmail,
					"You must a choose an existing user");
		} else if (flag == 2) {
			validation.addError(nameOrEmail,
					"You can't choose yourself (This is redundant!!!)");
		}
		if (validation.hasErrors()) {
			params.flash();
			validation.keep();
			organizerResignRequest(entityID);
		} else {
			EEntity entity = EEntity.findById(entityID);
			if (entity == null) {
				error("You are not authorized to view this page");
			}
			List<Request> requests = Request.findAll();
			boolean found = false;
			for (Request item : requests) {
				if (item.entity != null && item.entity.id == entityID
						&& item.sendingUser.id == sendingUser.id
						&& item.description != null
						&& item.description.equalsIgnoreCase("OrganizerResign")) {
					found = true;
					break;
				}
			}
			if (found) {
				int cancelRequest = 1;
				renderTemplate(
						"/RequestController/organizerResignRequest.html",
						cancelRequest, entity);
			}
			Request request = new Request(sendingUser, entity,
					entity.organization, recievingUser, new Date(),
					"OrganizerResign").save();
			String message = "The organizer of the entity "
					+ entity.name
					+ " wants to resign and suggested you to be this entity organizer "
					+ "\n Please log in and go to this link to accept ot reject the request:-"
					+ "\n http://localhost:9000/requestcontroller/showrequests";
			Application.sendMail(message, "Organizer Replacement",
					recievingUser);
            int flag2 = 0;
			renderTemplate("/RequestController/sendResignRequest.html", entityID,flag2);
		}
	}

	/**
	 * accepts a resign request from a specific organizer.So it makes the old
	 * organizer just an idea developer in the entity and assign the new
	 * organizer the organizer role with notifying the old organizer and the
	 * lead of the organization containing the entity and also all organizers in
	 * this entity.
	 * 
	 * @author Omar Darwish
	 * @param requestID
	 *            The id of the request that was accepted.
	 * @see a page specifying that the request has been accepted and the user
	 *      has been the new entity organizer.
	 * @story C1-S20
	 */
	public static void acceptOrganizerResignRequest(long requestID) {
		String userName = session.get("username");
		if (userName == null) {
			error("You are not authorized to view this page");
		}
		User user = User.find("byUsername", userName).first();
		Request request = Request.findById(requestID);
		if (user == null || request == null || request.recievingUser != user) {
			error("You are not authorized to view this page");
		}

		Role organizer = Role.find("byDescription", "Organizer").first();
		Role ideaDeveloper = Role.find("byDescription", "IdeaDeveloper")
				.first();
		RoleInEntity oldRoleOfOldOrganizer = RoleInEntity.find(
				"byRoleAndEntityAndUser", organizer, request.entity,
				request.sendingUser).first();
		oldRoleOfOldOrganizer.role = ideaDeveloper;
		oldRoleOfOldOrganizer.save();
		List<RoleInEntity> allRoles = RoleInEntity.findAll();
		boolean flag = false;
		for (RoleInEntity item : allRoles) {
			if (item.entity.id == request.entity.id
					&& item.user.id == request.recievingUser.id) {
				item.role = organizer;
				item.save();
				flag = true;
				break;
			}
		}
		if (!flag) {
			RoleInEntity newRole = new RoleInEntity(request.entity,
					request.recievingUser, organizer).save();
		}
		flag = false;
		List<RoleInOrganization> allRolesInOrganizations = RoleInOrganization.findAll();
		for (RoleInOrganization item : allRolesInOrganizations) {
			if (item.organization.id == request.entity.organization.id
					&& item.user.id == request.recievingUser.id) {
				flag = true;
				break;
			}
		}
		if(!flag){
			RoleInOrganization newRole = new RoleInOrganization(request.entity.organization,
					request.recievingUser, ideaDeveloper).save();
		}
		String message = "You are no longer the organizer of  "
				+ request.entity.name;
		Application.sendMail(message, "Organizer Replacement",
				request.sendingUser);
		EEntity entity = request.entity;
		List<User> Notified = entity.getOrganizers();
		Notified.add(entity.organization.getOrganizationLead());
		String message2 = request.sendingUser.name
				+ " chose to be no longer an organizer in " + entity.name
				+ " entity and " + request.recievingUser
				+ " is now the new organizer of this entity.";
		for (User item : Notified) {
			Application.sendMail(message2, "Organizer Replacement", item);
		}
		request.delete();
		int type = 2;
		renderTemplate("/RequestController/acceptResignRequest.html", entity,
				type);
	}

	/**
	 * ignores a resign request from a specific organizer and notifies the old
	 * organizer that the suggested one ignored the request.So the old organizer
	 * stays in his position
	 * 
	 * @author Omar Darwish
	 * @param requestID
	 *            The id of the request that was ignored.
	 * @see just the page that shows the requests of the navigating user.
	 * @story C1-S20
	 */
	public static void ignoreOrganizerResignRequest(long requestID) {
		String userName = session.get("username");
		if (userName == null) {
			error("You are not authorized to view this page");
		}
		User user = User.find("byUsername", userName).first();
		Request request = Request.findById(requestID);
		if (user == null || request == null) {
			error("You are not authorized to view this page");
		}
		String message = "The suggested user ignored you request so you are still  "
				+ "the organizer of " + request.entity.name + " entity";
		Application.sendMail(message, "Organizer Replacement",
				request.sendingUser);
		request.delete();
		showRequests();
	}

}
